package com.runshu.runshuwulian.base.baselistfragment;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.hyphenate.easeui.mwidget.brvahrecyclerview.BaseQuickAdapter;
import com.hyphenate.easeui.mwidget.brvahrecyclerview.RecycleViewDivider;
import com.runshu.runshuwulian.R;
import com.runshu.runshuwulian.base.BaseFragment;

import java.util.List;

import butterknife.BindView;

/**
 * 列表基类
 *
 * @param <A> adapter类名
 * @param <E> entity类
 */
abstract public class BaseListFragment<A extends BaseAdapter, E> extends BaseFragment implements BaseListView<E> {
    RecyclerView recyclerView;
    SwipeRefreshLayout swapLayout;
    BaseListHelper helper;

    public View emptyView;
    //默认支持翻页
    protected boolean hasPage = true;
    public BaseQuickAdapter mAdapter;

    protected void findAndInitRecyclerView(View v) {
        recyclerView = v.findViewById( R.id.recyclerview );
        swapLayout = v.findViewById( R.id.swap_layout );
        helper = getListViewHelper();
        hasPage = hasPage();
        mAdapter = getAdapter();
        recyclerView.setAdapter( mAdapter );
        recyclerView.setHasFixedSize( true );
        recyclerView.setLayoutManager( getLayoutManager() );
        int resId = getEmptyViewResId();
        if (resId == 0)
            emptyView = getLayoutInflater().inflate( R.layout.empty_view, (ViewGroup) recyclerView.getParent(), false );
        else
            emptyView = getLayoutInflater().inflate( resId, (ViewGroup) recyclerView.getParent(), false );
        emptyView.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                helper.refreshData();
            }
        } );
        swapLayout.setColorSchemeColors( getResources().getColor( R.color.bg_main_green ) );
        swapLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                helper.refreshData();
            }
        } );

        mAdapter.setOnLoadMoreListener( new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                helper.loadMore();
            }
        } );

        swapLayout.setRefreshing( true );
        BaseQuickAdapter.OnItemClickListener onItemClickListener = getOnItemClickListener();
        if (onItemClickListener != null)
            mAdapter.setOnItemClickListener( onItemClickListener );
        swapLayout.post( () -> {
            helper.refreshData();
        } );
    };


    @Override
    protected int setLayoutId() {
        return R.layout.frag_base_list;
    }

    @Override
    public void setListData(List<E> resultEntityList) {
        swapLayout.setRefreshing( false );
        mAdapter.setNewData( resultEntityList );
        mAdapter.loadMoreComplete();
        if (resultEntityList == null || resultEntityList.size() == 0) {
            mAdapter.setEmptyView( emptyView );
        }
        if (!hasPage) {
            mAdapter.loadMoreEnd( true );
        }
    }

    @Override
    public void addListData(List<E> resultEntityList) {
        swapLayout.setRefreshing( false );
        if (resultEntityList == null || resultEntityList.size() == 0) {
            mAdapter.loadMoreEnd();
            return;
        }
        mAdapter.addData( resultEntityList );
        mAdapter.loadMoreComplete();
    }

    @Override
    protected void initView() {
        super.initView();
        findAndInitRecyclerView( getView() );

    }


    protected abstract BaseQuickAdapter.OnItemClickListener getOnItemClickListener();

    protected abstract BaseListHelper getListViewHelper();

    public abstract <A extends BaseQuickAdapter> A getAdapter();

    protected abstract RecyclerView.LayoutManager getLayoutManager();

    protected boolean hasPage() {
        return true;
    }

    public void showDivider() {
        recyclerView.addItemDecoration( new RecycleViewDivider( selfAct(), LinearLayoutManager.HORIZONTAL,
                2, selfAct().getResources().getColor( R.color.color_eeeeee ) ) );
    }

    protected abstract int getEmptyViewResId();

}
